package com.sfzd5.exam;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.widget.LinearLayout;

import com.sfzd5.exam.helplibrary.exam.ExamInof;
import com.sfzd5.exam.helplibrary.exam.Page;
import com.sfzd5.exam.helplibrary.exam.PageA4;
import com.sfzd5.exam.helplibrary.exam.Rectangle;
import com.sfzd5.exam.helplibrary.exam.answersheet.Area;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class WorkProcess {

    ExamInof examInof;
    public List<File> pagePicFiles;
    public int curPage;

    public WorkProcess(ExamInof examInof){
        this.examInof = examInof;
        curPage = 0;
        pagePicFiles = new ArrayList<>();
    }

    public ExamInof getExamInof(){
        return examInof;
    }

    public Page getCurPage() {
        return examInof.pages.get(curPage);
    }
    public boolean isOptionsArea(int areaIdx){
        return examInof.areaList.get(areaIdx).areaType== Area.AreaType.Choice;
    }

    public List<Area> getAreas() {
        return examInof.areaList;
    }

    public Bitmap createCurPageFrontImage(double w, double h, double PageWidthRatio){
        Page page = getCurPage();
        double height = h * PageWidthRatio;
        double width = w * PageWidthRatio;

        if(width / height > PageA4.pageWidthm / PageA4.pageHeightm){
            width = height * PageA4.pageWidthm / PageA4.pageHeightm;
        } else {
            height = width * PageA4.pageHeightm / PageA4.pageWidthm;
        }

        Bitmap bmp2 = Bitmap.createBitmap((int)w, (int)h, Bitmap.Config.ARGB_4444);
        Canvas cv = new Canvas(bmp2);
        int backColor = 0x00000000;//透明
        cv.drawColor(backColor);

        Bitmap bmp = Bitmap.createBitmap((int)width, (int)height, Bitmap.Config.ARGB_4444);
        Canvas cv2 = new Canvas(bmp);
        cv2.drawColor(0x11FFFFFF);

        Paint paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.RED);

        //                宽度点数         210毫米
        //每毫米点数
        double mmPoints = width / PageA4.pageWidthm;
        double mmPoints2 = height / PageA4.pageHeightm;

        cv2.drawRect(makeLocationPointRectF(6, 11, 5, 5, mmPoints), paint);
        cv2.drawRect(makeLocationPointRectF(199, 11, 5, 5, mmPoints), paint);
        cv2.drawRect(makeLocationPointRectF(6, 281, 5, 5, mmPoints), paint);
        cv2.drawRect(makeLocationPointRectF(199, 281, 5, 5, mmPoints), paint);

        for(int i=0; i<5; i++){
            cv2.drawRect(makeLocationPointRectF(6, 49.5 * i + 49.5, 2, 5, mmPoints), paint);
        }

        List<Rectangle> rectangles = page.getRectangles(getAreas());
        paint.setStyle(Paint.Style.STROKE);
        for(Rectangle r : rectangles){
            cv2.drawRect(makeLocationPointRectF(r.x, r.y, r.w, r.h, mmPoints), paint);
        }

        cv.drawBitmap(bmp, (int)((w-width)/2), (int)((h-height)/2), paint);

        return  bmp2;
    }

    RectF makeLocationPointRectF(double left, double top, double width, double height, double mmPoints){
        RectF r = new RectF();
        r.left = (float)(left*mmPoints);
        r.top = (float) (top*mmPoints);
        r.right = r.left + (float) (width*mmPoints);
        r.bottom = r.top + (float) (height*mmPoints);
        return r;
    }

}
